---
import { getCollection } from 'astro:content';
import { config_site } from '../../utils/config-adapter';
import { processFrontmatter } from '../../integrations/process-frontmatter';
import { extractFlatCategories, sortCategoriesByCount } from '../../utils/category-utils';
import CategoriesLayout from '../../layouts/CategoriesLayout.astro';

// 获取文章集合
const allPosts = await getCollection('posts');
const processedPosts = await Promise.all(allPosts.map(post => processFrontmatter(post)));

// 获取所有分类并按文章数量排序
const categories = extractFlatCategories(processedPosts);
const sortedCategories = sortCategoriesByCount(categories);

// 页面标题
const pageTitle = '文章分类 | ' + config_site.siteName;
---

<CategoriesLayout 
  title={pageTitle}
  description="所有文章分类列表，按主题分类索引"
  author={config_site.author || ''}
  url={config_site.url + '/categories/'}
  categories={sortedCategories}
  noIndex={false}
/>
